package cn.yedong.pet.service.impl;

import cn.yedong.basic.service.IBaiduAiAuditService;
import cn.yedong.basic.util.AjaxResult;
import cn.yedong.pet.domain.Pet;
import cn.yedong.pet.domain.PetDetail;
import cn.yedong.pet.domain.PetOnlineAuditLog;
import cn.yedong.pet.mapper.PetDetailMapper;
import cn.yedong.pet.mapper.PetMapper;
import cn.yedong.pet.service.IPetOnlineAuditLogService;
import cn.yedong.pet.service.IPetService;
import cn.yedong.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private IPetOnlineAuditLogService petOnlineAuditLogService;

    /**
     * 添加宠物时候，添加宠物详情
     * @param pet
     */
    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    /**
     * 删除宠物之前，删除宠物详情
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        petDetailMapper.delPetDetailByPetId(id);
        super.del(id);
    }

    /**
     * 修改宠物之前，修改宠物详情
     * 注意：修改宠物详情的时候不要修改宠物的id
     * @param pet
     */
    @Transactional
    @Override
    public void update(Pet pet) {
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            //当petDetail的id不存在时，将当前pet的id设置给petDetail，执行添加操作
            //场景：放入寻主池的pet再次修改富文本的时候
            if(petDetail.getId() == null){
                petDetail.setPetId(pet.getId());
                petDetailMapper.add(petDetail);
            }else {
                //注意：【改写sql语句】修改宠物详情的时候不要修改petId
                petDetailMapper.update(petDetail);
            }
        }
        super.update(pet);
    }

    /**
     * 删除每一个宠物之前，删除其宠物详情
     * @param ids
     */
    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    /**
     * 商品上架 - 百度AI智能审核 宠物名+图片
     * @param ids
     */
    @Transactional
    @Override
    public AjaxResult onSale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //百度智能名字审核
            Boolean nameBool = baiduAiAuditService.textAudit(pet.getName());
            //百度智能图像审核
            Boolean imgBool = baiduAiAuditService.imageAudit(pet.getResources());
            //两者都审核通过
            if(nameBool && imgBool){
                //添加上架时间；设置状态为 1
                petMapper.updateOnsaleState(id,new Date());
                //添加审核通过日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setNote(id+"审核通过！");
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLogService.add(petOnlineAuditLog);
            }else {
                //添加审核失败日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setNote(id+"审核失败！");
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLogService.add(petOnlineAuditLog);
                return new AjaxResult(false,"信息审核失败！");
            }
        }
        return new AjaxResult();
    }

    /**
     * 商品下架
     * @param ids
     */
    @Transactional
    @Override
    public void offSale(List<Long> ids) {
        petMapper.offSale(ids,new Date());
    }
}
